package com.ysqcould.web.controller.message;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ysqcould.common.annotation.Log;
import com.ysqcould.common.core.controller.BaseController;
import com.ysqcould.common.core.domain.AjaxResult;
import com.ysqcould.common.core.page.TableDataInfo;
import com.ysqcould.common.enums.BusinessType;
import com.ysqcould.common.utils.poi.ExcelUtil;
import com.ysqcould.message.domain.Sms;
import com.ysqcould.message.domain.SmsApp;
import com.ysqcould.message.mapper.SmsAppMapper;
import com.ysqcould.message.mapper.SmsMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ysqcould.message.domain.SmsContent;
import com.ysqcould.message.service.ISmsContentService;

import static com.ysqcould.message.utils.sms.SmsUtil.sendSmsMessage;

/**
 * 短信内容Controller
 * 
 * @author jcd
 * @date 2022-09-21
 */
@RestController
@RequestMapping("/message/content")
public class SmsContentController extends BaseController
{
    @Autowired
    private ISmsContentService smsContentService;

    @Autowired
    private SmsAppMapper smsAppMapper;

    @Autowired
    private SmsMapper smsMapper;

    /**
     * 查询短信内容列表
     */
    @PreAuthorize("@ss.hasPermi('message:content:list')")
    @GetMapping("/list")
    public TableDataInfo list(SmsContent smsContent)
    {
        startPage();
        List<SmsContent> list = smsContentService.selectSmsContentList(smsContent);
        return getDataTable(list);
    }

    /**
     * 导出短信内容列表
     */
    @PreAuthorize("@ss.hasPermi('message:content:export')")
    @Log(title = "短信内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmsContent smsContent)
    {
        List<SmsContent> list = smsContentService.selectSmsContentList(smsContent);
        ExcelUtil<SmsContent> util = new ExcelUtil<SmsContent>(SmsContent.class);
        util.exportExcel(response, list, "短信内容数据");
    }

    /**
     * 获取短信内容详细信息
     */
    @PreAuthorize("@ss.hasPermi('message:content:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        AjaxResult result= AjaxResult.success();
        Long userId = getUserId();
        Long asId=null;
        if (userId==1){
            userId=asId;
        }
        SmsApp app=new SmsApp();
        app.setAsId(userId);
        List<SmsApp> smsApps = smsAppMapper.selectSmsAppList(app);
        Sms sms=new Sms();
        List<Sms> list = smsMapper.selectSmsList(sms);
        SmsContent smsContent = smsContentService.selectSmsContentById(id);
        result.put("smsApps",smsApps);
        result.put("list",list);
        result.put("smsContent",smsContent);
        return result;
    }

    /**
     * 新增短信内容
     */
    @PreAuthorize("@ss.hasPermi('message:content:add')")
    @Log(title = "短信内容", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmsContent smsContent)
    {
        return toAjax(smsContentService.insertSmsContent(smsContent));
    }

    /**
     * 修改短信内容
     */
    @PreAuthorize("@ss.hasPermi('message:content:edit')")
    @Log(title = "短信内容", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmsContent smsContent)
    {
       return smsContentService.updateSmsContent(smsContent);
    }

    /**
     * 删除短信内容
     */
    @PreAuthorize("@ss.hasPermi('message:content:remove')")
    @Log(title = "短信内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(smsContentService.deleteSmsContentByIds(ids));
    }
}
